﻿package com.manyiaby.view.viewutils;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by yx on 2018/08/14
 */
public class CodeUtil {


    /**
     * url编码
     *UTF-8
     * @param str
     * @return
     */
    public static String getStringEncode(String str) {
        String result="";
        try {
            result=URLEncoder.encode(str, "UTF-8");
        } catch (Exception e) {//UnsupportedEncodingException
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return result;
    }

    /**
     * url解码
     *UTF-8
     * @param str
     * @return
     */
    public static String getStringDecode(String str) {
        String result="";
        try {
            if(!TextUtils.isEmpty(str)){
                result=URLDecoder.decode(str, "UTF-8");
            }else{
                result=str;
            }
            LogUtil.i("decode","================"+result);
        } catch (Exception e) {//UnsupportedEncodingException
            result=str;
            LogUtil.e("decode","decode="+result);
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 是否为有效url
     * @param urlStr
     * @return
     */
    public static boolean isUrl(String urlStr){
        if(!TextUtils.isEmpty(urlStr)&&(urlStr.contains("http:////")||urlStr.contains("https:////"))){
            return true;
        }else{
            return false;
        }

    }

}
